/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.service;

import com.jmooc.dao.QuestionDao;
import com.jmooc.entity.Course;
import com.jmooc.entity.Question;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Group
 */
@Stateless
public class QuestionService implements Serializable {
     
    @EJB
    private QuestionDao questionDao;
    
    // Return randomly ordered questions
    public List<Question> getByCourse(Course course) {
        List<Question> result = questionDao.getAllByCourse(course);
        
        for (int i = 0; i < 5 * result.size(); ++i) {
            int pos1 = (int) Math.ceil(Math.random() * result.size())-1;
            int pos2 = (int) Math.ceil(Math.random() * result.size())-1;
            
            Question tmp = result.get(pos1);
            
            result.set(pos1, result.get(pos2));
            result.set(pos2, tmp);
        }
        
        return result;
    }
    
    public void create(Question question) {
        questionDao.create(question);
    }
    
    public void update(Question question) {
        questionDao.update(question);
    }
}
